package com.fanhao.dllo.justforyou.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.fanhao.dllo.justforyou.R;
import com.fanhao.dllo.justforyou.entity.greendao.PersonEntity;
import com.fanhao.dllo.justforyou.helper.PersonHelper;

import java.util.List;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView ImgIv;
    private EditText nameEt, pswEt;
    private Button registerBtn;

    //helper 帮助类
    private PersonHelper helper;
    private List<PersonEntity> personquery;

    private String name;
    private String psw;
    private boolean flag = false;
    private int count = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);


        ImgIv = (ImageView) findViewById(R.id.imgIv);
        nameEt = (EditText) findViewById(R.id.nameEt);
        pswEt = (EditText) findViewById(R.id.pswEt);
        registerBtn = (Button) findViewById(R.id.registerBtn);


        helper = PersonHelper.getInstance();
        //清除数据库
        //  helper.DeleteAll();

        // 定义查询所有数据对象
        personquery = helper.queryAll();
        registerBtn.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

                name = nameEt.getText().toString();
                psw = pswEt.getText().toString();
                PersonEntity personEntity = new PersonEntity(null, name, psw);

                flag = helper.getName(name);

                if (name != null && name.length() > 0) {
                    if (flag == false) {
                        if (psw != null && psw.length() > 0) {
                            Toast.makeText(this, "恭喜你注册成功", Toast.LENGTH_SHORT).show();

                            helper.savePersonEntityList(personEntity);

                            Intent intent = new Intent(this, LoginActivity.class);
                            startActivity(intent);

                            //左进右出动画效果
                            overridePendingTransition(
                                    R.anim.left_into, R.anim.right_out);
                            finish();

                        } else {
                            Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        Toast.makeText(this, "用户名已被注册", Toast.LENGTH_SHORT).show();
                    }

                } else {
                    Toast.makeText(this, "用户名不能为空", Toast.LENGTH_SHORT).show();
                }


        }


    }

